package org.example.springweibo.service;

import org.example.springweibo.exception.ServiceException;
import org.example.springweibo.mapper.WeiboMapper;
import org.example.springweibo.pojo.entity.Weibo;
import org.example.springweibo.pojo.vo.WeiboCommentVO;
import org.example.springweibo.pojo.vo.WeiboVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class WeiboService {

    @Autowired
    private WeiboMapper weiboMapper;

    public List<WeiboCommentVO> getWeiboList() {
        //List<对象(用户名 微博发布时间 内容 评论数量 List<对象(评论的用户名 评论内容  评论时间)>)>
        List<WeiboCommentVO> weiboList = weiboMapper.getWeiboList(null);
        for (WeiboCommentVO weiboCommentVO : weiboList) {
            weiboCommentVO.setCommentSize(weiboCommentVO.getCommentList().size());
        }
        return weiboList;
    }

    public List<WeiboCommentVO> getWeiboListByContent(Weibo weibo) {
        //List<对象(用户名 微博发布时间 内容 评论数量 List<对象(评论的用户名 评论内容  评论时间)>)>
        List<WeiboCommentVO> weiboList = weiboMapper.getWeiboList(weibo.getContent());
        for (WeiboCommentVO weiboCommentVO : weiboList) {
            weiboCommentVO.setCommentSize(weiboCommentVO.getCommentList().size());
        }
        return weiboList;
    }

    public int addWeibo(Weibo weibo){
        weibo.setCreated(new Date());
        return weiboMapper.addWeibo(weibo);
    }

    public List<WeiboVO> getWeibos(int uId) {
        return weiboMapper.getWeibos(uId);
    }

    public int updateWeibo(Weibo weibo) {
        weibo.setCreated(new Date());
        return weiboMapper.updateWeibo(weibo);
    }

    @Transactional
    public int deleteWeibo(int id) {//删除微博
        if (weiboMapper.deleteWeibo(id) != 1 && weiboMapper.deleteComment(id) != 1) {
            //删除失败
        }
        return 1;
    }

    public int deleteComment(int id) {
        return weiboMapper.deleteCommentById(id);
    }
}
